package com.tuuzed.hydrogen.simple.quickstart;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.tuuzed.adapter.RecyclerViewAdapter;
import com.tuuzed.hydrogen.simple.R;
import com.tuuzed.hydrogen.simple.model.UserModel;

import java.util.List;

public class QuickStartActivity extends AppCompatActivity {
    private static final String TAG = "QuickStartActivity";
    private RecyclerViewAdapter adapter;

    @Override
    protected void onRestart() {
        super.onRestart();
        adapter.items().clear();
        List<UserModel> users = UserModel.findAll(UserModel.class);
        adapter.items().addAll(users);
        adapter.notifyDataSetChanged();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_start);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(QuickStartActivity.this, UserActivity.class);
                startActivity(intent);
            }
        });
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecyclerViewAdapter(recyclerView);
        adapter.register(UserModel.class, new UserItemProvider());
        List<UserModel> users = UserModel.findAll(UserModel.class);
        Log.d(TAG, "onCreate: " + users);
        adapter.items().addAll(users);
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

}
